package com.my.lucky.ui;

import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationContext;

import javax.swing.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class WindowCloseListener extends WindowAdapter {
    private final ApplicationContext applicationContext;
    private final JFrame mainFrame;

    public WindowCloseListener(ApplicationContext applicationContext, JFrame mainFrame) {
        this.applicationContext = applicationContext;
        this.mainFrame = mainFrame;
    }

    @Override
    public void windowClosing(WindowEvent e) {
        int result = JOptionPane.showConfirmDialog(
                mainFrame,
                "确定要退出程序吗？",
                "退出确认",
                JOptionPane.YES_NO_OPTION,
                JOptionPane.QUESTION_MESSAGE
        );

        if (result == JOptionPane.YES_OPTION) {
            // 正常关闭Spring Boot应用
            int exitCode = SpringApplication.exit(applicationContext, () -> 0);
            System.exit(exitCode);
        }
        // 如果用户选择"否"，什么都不做，窗口继续保持打开状态
    }
} 